package com.csii.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.org.apache.bcel.internal.classfile.StackMapType;

public class HttpSDKRequest {

	
	public static void main(String[] args) throws Exception {
		mainSendQueryFee();
	}
	
	
	
	public static void mainSendQueryFee() throws Exception{
		//查询停车费
		String surl = "http://localhost:8080/QRC/sdkconnect.do";
		
		String gb = "UTF-8";
		String params = getParamStr();
		
		 StringBuffer sb = new StringBuffer();
	        HttpURLConnection uc = null;
	        HttpsURLConnection ucs = null;
	        try {
	        	System.out.println("请求URL:"+surl);
	        	System.out.println("请求参数："+params);
	        	URL url= new URL(surl);
	            BufferedReader in = null;
	            SSLContext sc = SSLContext.getInstance("SSL");
	        	if ("https".equals(url.getProtocol())) {
	        		ucs = (HttpsURLConnection) url.openConnection();
	    			ucs.setSSLSocketFactory(sc.getSocketFactory());
	    			ucs.setHostnameVerifier(new MyHostnameVerifier());
	    			uc = ucs;
	    		} else {
	    			uc = (HttpURLConnection) url.openConnection();
	    		}
	        	
	        	
	            sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
	            
	            uc.setRequestMethod("POST");
	            uc.setDoOutput(true);
	            uc.setDoInput(true);
	            uc.setUseCaches(false);
	            uc.setConnectTimeout(100000);
	            uc.setRequestProperty("Content-Type", "application/json");
	            uc.connect();
	            DataOutputStream out = new DataOutputStream(uc.getOutputStream());
	            out.write(params.getBytes(gb));
	            out.flush();
	            out.close();
	            in = new BufferedReader(new InputStreamReader(uc.getInputStream(), gb));
	            String readLine = "";
	            while ((readLine = in.readLine()) != null) {
	                sb.append(readLine).append("\t\n");
	            }
	            if (in != null) {
	                in.close();
	            }
	            if (uc != null) {
	                uc.disconnect();
	            }
	        } catch (Exception e) {
	        	System.out.println(e.getMessage());
	        } finally {
	            if (uc != null) {
	                uc.disconnect();
	            }
	        }
	        
	       System.out.println("====返回===："+sb+"");
	       
	       Map  sMap = (Map)JSON.parse(sb.toString());
	       System.out.println("返回json数据为："+sMap.toString());
	}
	
	
	/**
     * HTTP协议POST请求添加参数的封装方?
	 * @throws Exception 
     */
    private static String getParamStr() throws Exception {
        Map jsonMap = new HashMap();
        jsonMap.put("TransId", "SD01");
        jsonMap.put("MerchantId", "95162064215P000");
        
        String sJson = toJsonStr(jsonMap);
    	
    	
    	Map sendMap = new HashMap();
        sendMap.put("DESStr", RDes.encrypt(sJson));
        sendMap.put("MD5Str", MD5.sign(sJson));
    	
    	return toJsonStr(sendMap);
    }
    
    
    
    public static String toJsonStr(Map dataMap) {
		Gson gson = new GsonBuilder().disableHtmlEscaping().create();
		return gson.toJson(dataMap);
	}
    
    
    private static class MyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
        	//return false;		//生产环境默认返回false
			return true;	//测试环境默认返回true
        }
    }
    
    
    private static class MyTrustManager implements X509TrustManager {
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[]{};
        }
    }
}
